// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import SkillManager, { SkillEvent } from "../../Models/Skills/SkillManager";
import BaseUI from "../BaseUI";
import SkillItem from "../UIItems/SkillItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SkillUI extends BaseUI {
    @property(cc.Prefab)
    skillitemPrefab: cc.Prefab = null;
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    onLoad() {
        var skillArr = SkillManager.single.getSkillArr();
        for (let i = 0; i < skillArr.length; i++) {
            const element = skillArr[i];
            var item = cc.instantiate(this.skillitemPrefab);
            item.active = false;
            this.scrollview.content.addChild(item);
        }

    }

    protected onEnable(): void {
        this.drawView();

        GameEvent.eventNode.on(SkillEvent.SKILL_UNLOCK_EVENT, this.onUpdataUi, this);
        GameEvent.eventNode.on(SkillEvent.SKILL_UPGRADE_EVENT, this.onUpdataUi, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(SkillEvent.SKILL_UNLOCK_EVENT, this.onUpdataUi, this);
        GameEvent.eventNode.off(SkillEvent.SKILL_UPGRADE_EVENT, this.onUpdataUi, this);
    }
    onUpdataUi() {
        this.drawView();
    }
    drawView() {

        var arr: cc.Node[] = this.scrollview.content.children;
        var skillArr = SkillManager.single.getSkillArr();
        for (let i = 0; i < arr.length; i++) {
            const element = arr[i];
            element.active = true;
            element.getComponent(SkillItem).drawItem(skillArr[i].id);
        }
    }

}
